<?php
class ControllerModuleDownload extends Controller {
    public function index() {
        $this->load->model('catalog/download');
        $download_info = $this->model_catalog_download->getDownload(1);
        if ($download_info) {
            $file = DIR_DOWNLOAD . $download_info['filename'];
            $mask = basename($download_info['mask']);

            if (!headers_sent()) {
                if (file_exists($file)) {
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($file));

                    if (ob_get_level()) ob_end_clean();

                    readfile($file, 'rb');

                    $this->model_account_download->updateRemaining($this->request->get['order_download_id']);

                    exit;
                } else {
                    exit('Error: Could not find file ' . $file . '!');
                }
            } else {
                exit('Error: Headers already sent out!');
            }
        } else {
            $this->redirect($this->url->link('common/home'));
        }
    }

    public function info() {
        $this->load->model('catalog/download');

        $result = $this->model_catalog_download->getDownload(1);
        $this->data['downloads'][] = array(
            'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
            'name'       => $result['name'],
            'href'       => $this->url->link('module/download/download', 'download_id=' . $result['download_id'])
        );

        $this->response->setOutput($this->render());
    }
}